﻿Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace Othello
    ''' <summary>
    ''' Summary description for OptionsDialog.
    ''' </summary>
    Public Class OptionsDialog
        Inherits System.Windows.Forms.Form
        Private optionsTabControl As System.Windows.Forms.TabControl
        Private displayTabPage As System.Windows.Forms.TabPage
        Private showValidMovesCheckBox As System.Windows.Forms.CheckBox
        Private previewMovesCheckBox As System.Windows.Forms.CheckBox
        Private boardColorLabel As System.Windows.Forms.Label
        Private boardColorPanel As System.Windows.Forms.Panel
        Private WithEvents boardColorButton As System.Windows.Forms.Button
        Private validColorLabel As System.Windows.Forms.Label
        Private validColorPanel As System.Windows.Forms.Panel
        Private WithEvents validColorButton As System.Windows.Forms.Button
        Private activeColorLabel As System.Windows.Forms.Label
        Private activeColorPanel As System.Windows.Forms.Panel
        Private WithEvents activeColorButton As System.Windows.Forms.Button
        Private moveIndicatorColorLabel As System.Windows.Forms.Label
        Private moveIndicatorColorPanel As System.Windows.Forms.Panel
        Private WithEvents moveIndicatorColorButton As System.Windows.Forms.Button
        'Private firstMoveLabel As System.Windows.Forms.Label
        'Private firstMovePanel As System.Windows.Forms.Panel
        'Private firstMoveBlackRadioButton As System.Windows.Forms.RadioButton
        'Private firstMoveWhiteRadioButton As System.Windows.Forms.RadioButton
        'Private playersTabPage As System.Windows.Forms.TabPage


        Private WithEvents restoreDefaultsButton As System.Windows.Forms.Button
        Private WithEvents okButton As System.Windows.Forms.Button
        Private Shadows WithEvents cancelButton As System.Windows.Forms.Button

        ''' <summary>
        ''' Required designer variable.
        ''' </summary>
        ''' 
        Private components As System.ComponentModel.Container = Nothing

        ' The game options.
        Public Options As Options

        ' An array to store custom colors added by the user.
        Private Shared customColors As Integer() = New Integer() {}

        Public Sub New(ByVal options As Options)
            '
            ' Required for Windows Form Designer support
            '
            InitializeComponent()

            '
            ' TODO: Add any constructor code after InitializeComponent call
            '

            ' Create a copy of the given game options.
            Me.Options = New Options(options)

            ' Set the form controls based on those options.
            Me.MapOptionsToControls()
        End Sub

        ''' <summary>
        ''' Clean up any resources being used.
        ''' </summary>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Windows Form Designer generated code"
        ''' <summary>
        ''' Required method for Designer support - do not modify
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
            Me.optionsTabControl = New System.Windows.Forms.TabControl
            Me.displayTabPage = New System.Windows.Forms.TabPage
            Me.previewMovesCheckBox = New System.Windows.Forms.CheckBox
            Me.moveIndicatorColorButton = New System.Windows.Forms.Button
            Me.moveIndicatorColorPanel = New System.Windows.Forms.Panel
            Me.moveIndicatorColorLabel = New System.Windows.Forms.Label
            Me.validColorButton = New System.Windows.Forms.Button
            Me.validColorPanel = New System.Windows.Forms.Panel
            Me.validColorLabel = New System.Windows.Forms.Label
            Me.activeColorButton = New System.Windows.Forms.Button
            Me.activeColorPanel = New System.Windows.Forms.Panel
            Me.activeColorLabel = New System.Windows.Forms.Label
            Me.boardColorButton = New System.Windows.Forms.Button
            Me.boardColorPanel = New System.Windows.Forms.Panel
            Me.boardColorLabel = New System.Windows.Forms.Label
            Me.showValidMovesCheckBox = New System.Windows.Forms.CheckBox
            Me.okButton = New System.Windows.Forms.Button
            Me.cancelButton = New System.Windows.Forms.Button
            Me.restoreDefaultsButton = New System.Windows.Forms.Button
            Me.optionsTabControl.SuspendLayout()
            Me.displayTabPage.SuspendLayout()
            Me.SuspendLayout()
            '
            'optionsTabControl
            '
            Me.optionsTabControl.Controls.Add(Me.displayTabPage)
            Me.optionsTabControl.Location = New System.Drawing.Point(8, 16)
            Me.optionsTabControl.Name = "optionsTabControl"
            Me.optionsTabControl.SelectedIndex = 0
            Me.optionsTabControl.Size = New System.Drawing.Size(288, 216)
            Me.optionsTabControl.TabIndex = 0
            '
            'displayTabPage
            '
            Me.displayTabPage.Controls.Add(Me.previewMovesCheckBox)
            Me.displayTabPage.Controls.Add(Me.moveIndicatorColorButton)
            Me.displayTabPage.Controls.Add(Me.moveIndicatorColorPanel)
            Me.displayTabPage.Controls.Add(Me.moveIndicatorColorLabel)
            Me.displayTabPage.Controls.Add(Me.validColorButton)
            Me.displayTabPage.Controls.Add(Me.validColorPanel)
            Me.displayTabPage.Controls.Add(Me.validColorLabel)
            Me.displayTabPage.Controls.Add(Me.activeColorButton)
            Me.displayTabPage.Controls.Add(Me.activeColorPanel)
            Me.displayTabPage.Controls.Add(Me.activeColorLabel)
            Me.displayTabPage.Controls.Add(Me.boardColorButton)
            Me.displayTabPage.Controls.Add(Me.boardColorPanel)
            Me.displayTabPage.Controls.Add(Me.boardColorLabel)
            Me.displayTabPage.Controls.Add(Me.showValidMovesCheckBox)
            Me.displayTabPage.Location = New System.Drawing.Point(4, 22)
            Me.displayTabPage.Name = "displayTabPage"
            Me.displayTabPage.Size = New System.Drawing.Size(280, 190)
            Me.displayTabPage.TabIndex = 0
            Me.displayTabPage.Text = "Display"
            '
            'previewMovesCheckBox
            '
            Me.previewMovesCheckBox.Location = New System.Drawing.Point(144, 10)
            Me.previewMovesCheckBox.Name = "previewMovesCheckBox"
            Me.previewMovesCheckBox.Size = New System.Drawing.Size(104, 24)
            Me.previewMovesCheckBox.TabIndex = 1
            Me.previewMovesCheckBox.Text = "Preview moves"
            '
            'moveIndicatorColorButton
            '
            Me.moveIndicatorColorButton.Location = New System.Drawing.Point(175, 157)
            Me.moveIndicatorColorButton.Name = "moveIndicatorColorButton"
            Me.moveIndicatorColorButton.Size = New System.Drawing.Size(75, 23)
            Me.moveIndicatorColorButton.TabIndex = 14
            Me.moveIndicatorColorButton.Text = "Select..."
            '
            'moveIndicatorColorPanel
            '
            Me.moveIndicatorColorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.moveIndicatorColorPanel.Location = New System.Drawing.Point(143, 160)
            Me.moveIndicatorColorPanel.Name = "moveIndicatorColorPanel"
            Me.moveIndicatorColorPanel.Size = New System.Drawing.Size(16, 16)
            Me.moveIndicatorColorPanel.TabIndex = 13
            '
            'moveIndicatorColorLabel
            '
            Me.moveIndicatorColorLabel.AutoSize = True
            Me.moveIndicatorColorLabel.Location = New System.Drawing.Point(27, 162)
            Me.moveIndicatorColorLabel.Name = "moveIndicatorColorLabel"
            Me.moveIndicatorColorLabel.Size = New System.Drawing.Size(106, 13)
            Me.moveIndicatorColorLabel.TabIndex = 12
            Me.moveIndicatorColorLabel.Text = "Move indicator color:"
            '
            'validColorButton
            '
            Me.validColorButton.Location = New System.Drawing.Point(175, 93)
            Me.validColorButton.Name = "validColorButton"
            Me.validColorButton.Size = New System.Drawing.Size(75, 23)
            Me.validColorButton.TabIndex = 8
            Me.validColorButton.Text = "Select..."
            '
            'validColorPanel
            '
            Me.validColorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.validColorPanel.Location = New System.Drawing.Point(143, 96)
            Me.validColorPanel.Name = "validColorPanel"
            Me.validColorPanel.Size = New System.Drawing.Size(16, 16)
            Me.validColorPanel.TabIndex = 7
            '
            'validColorLabel
            '
            Me.validColorLabel.AutoSize = True
            Me.validColorLabel.Location = New System.Drawing.Point(45, 98)
            Me.validColorLabel.Name = "validColorLabel"
            Me.validColorLabel.Size = New System.Drawing.Size(88, 13)
            Me.validColorLabel.TabIndex = 6
            Me.validColorLabel.Text = "Valid move color:"
            '
            'activeColorButton
            '
            Me.activeColorButton.Location = New System.Drawing.Point(175, 125)
            Me.activeColorButton.Name = "activeColorButton"
            Me.activeColorButton.Size = New System.Drawing.Size(75, 23)
            Me.activeColorButton.TabIndex = 11
            Me.activeColorButton.Text = "Select..."
            '
            'activeColorPanel
            '
            Me.activeColorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.activeColorPanel.Location = New System.Drawing.Point(143, 128)
            Me.activeColorPanel.Name = "activeColorPanel"
            Me.activeColorPanel.Size = New System.Drawing.Size(16, 16)
            Me.activeColorPanel.TabIndex = 10
            '
            'activeColorLabel
            '
            Me.activeColorLabel.AutoSize = True
            Me.activeColorLabel.Location = New System.Drawing.Point(32, 130)
            Me.activeColorLabel.Name = "activeColorLabel"
            Me.activeColorLabel.Size = New System.Drawing.Size(101, 13)
            Me.activeColorLabel.TabIndex = 9
            Me.activeColorLabel.Text = "Active square color:"
            '
            'boardColorButton
            '
            Me.boardColorButton.Location = New System.Drawing.Point(175, 61)
            Me.boardColorButton.Name = "boardColorButton"
            Me.boardColorButton.Size = New System.Drawing.Size(75, 23)
            Me.boardColorButton.TabIndex = 5
            Me.boardColorButton.Text = "Select..."
            '
            'boardColorPanel
            '
            Me.boardColorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.boardColorPanel.Location = New System.Drawing.Point(143, 64)
            Me.boardColorPanel.Name = "boardColorPanel"
            Me.boardColorPanel.Size = New System.Drawing.Size(16, 16)
            Me.boardColorPanel.TabIndex = 4
            '
            'boardColorLabel
            '
            Me.boardColorLabel.AutoSize = True
            Me.boardColorLabel.Location = New System.Drawing.Point(71, 66)
            Me.boardColorLabel.Name = "boardColorLabel"
            Me.boardColorLabel.Size = New System.Drawing.Size(64, 13)
            Me.boardColorLabel.TabIndex = 3
            Me.Text = "Board color:"
            '
            'showValidMovesCheckBox
            '
            Me.showValidMovesCheckBox.Location = New System.Drawing.Point(16, 10)
            Me.showValidMovesCheckBox.Name = "showValidMovesCheckBox"
            Me.showValidMovesCheckBox.Size = New System.Drawing.Size(120, 24)
            Me.showValidMovesCheckBox.TabIndex = 0
            Me.showValidMovesCheckBox.Text = "Show valid moves"
            '
            'okButton
            '
            Me.okButton.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.okButton.Location = New System.Drawing.Point(136, 240)
            Me.okButton.Name = "okButton"
            Me.okButton.Size = New System.Drawing.Size(75, 23)
            Me.okButton.TabIndex = 1
            Me.okButton.Text = "OK"
            '
            'cancelButton
            '
            Me.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.cancelButton.Location = New System.Drawing.Point(221, 240)
            Me.cancelButton.Name = "cancelButton"
            Me.cancelButton.Size = New System.Drawing.Size(75, 23)
            Me.cancelButton.TabIndex = 2
            Me.cancelButton.Text = "Cancel"
            '
            'restoreDefaultsButton
            '
            Me.restoreDefaultsButton.Location = New System.Drawing.Point(8, 240)
            Me.restoreDefaultsButton.Name = "restoreDefaultsButton"
            Me.restoreDefaultsButton.Size = New System.Drawing.Size(96, 23)
            Me.restoreDefaultsButton.TabIndex = 3
            Me.restoreDefaultsButton.Text = "Restore Defaults"
            '
            'OptionsDialog
            '
            Me.AcceptButton = Me.okButton
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(306, 270)
            Me.ControlBox = False
            Me.Controls.Add(Me.restoreDefaultsButton)
            Me.Controls.Add(Me.cancelButton)
            Me.Controls.Add(Me.okButton)
            Me.Controls.Add(Me.optionsTabControl)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "OptionsDialog"
            Me.ShowInTaskbar = False
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
            Me.Text = "Options"
            Me.optionsTabControl.ResumeLayout(False)
            Me.displayTabPage.ResumeLayout(False)
            Me.displayTabPage.PerformLayout()
            Me.ResumeLayout(False)

        End Sub
#End Region

        '
        ' Sets the form controls based on the current game options.
        '
        Private Sub MapOptionsToControls()
            Me.showValidMovesCheckBox.Checked = Me.Options.ShowValidMoves
            Me.previewMovesCheckBox.Checked = Me.Options.PreviewMoves
            Me.boardColorPanel.BackColor = Me.Options.BoardColor
            Me.validColorPanel.BackColor = Me.Options.ValidMoveColor
            Me.activeColorPanel.BackColor = Me.Options.ActiveSquareColor
            Me.moveIndicatorColorPanel.BackColor = Me.Options.MoveIndicatorColor

            Me.Refresh()
        End Sub

        '
        ' Sets the game options based on the current state of the form
        ' controls.
        '
        Private Sub MapControlsToOptions()
            Me.Options.ShowValidMoves = Me.showValidMovesCheckBox.Checked
            Me.Options.PreviewMoves = Me.previewMovesCheckBox.Checked
            Me.Options.BoardColor = Me.boardColorPanel.BackColor
            Me.Options.ValidMoveColor = Me.validColorPanel.BackColor
            Me.Options.ActiveSquareColor = Me.activeColorPanel.BackColor
            Me.Options.MoveIndicatorColor = Me.moveIndicatorColorPanel.BackColor

        End Sub

        ' ===================================================================
        ' Event handlers for the color select buttons.
        ' ===================================================================

        Private Sub boardColorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles boardColorButton.Click
            ' Open a color dialog.
            Dim dlg As New ColorDialog()
            dlg.Color = Me.boardColorPanel.BackColor
            dlg.CustomColors = OptionsDialog.customColors

            ' Set the board color based on that selection.
            If dlg.ShowDialog(Me) = DialogResult.OK Then
                Me.boardColorPanel.BackColor = dlg.Color
                Me.boardColorPanel.Refresh()
                OptionsDialog.customColors = dlg.CustomColors
            End If
        End Sub

        Private Sub validColorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles validColorButton.Click
            ' Open a color dialog.
            Dim dlg As New ColorDialog()
            dlg.Color = Me.validColorPanel.BackColor
            dlg.CustomColors = OptionsDialog.customColors

            ' Set the valid move color based on that selection.
            If dlg.ShowDialog(Me) = DialogResult.OK Then
                Me.validColorPanel.BackColor = dlg.Color
                Me.validColorPanel.Refresh()
                OptionsDialog.customColors = dlg.CustomColors
            End If
        End Sub

        Private Sub activeColorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles activeColorButton.Click
            ' Open a color dialog.
            Dim dlg As New ColorDialog()
            dlg.Color = Me.activeColorPanel.BackColor
            dlg.CustomColors = OptionsDialog.customColors

            ' Set the active square color based on that selection.
            If dlg.ShowDialog(Me) = DialogResult.OK Then
                Me.activeColorPanel.BackColor = dlg.Color
                Me.activeColorPanel.Refresh()
                OptionsDialog.customColors = dlg.CustomColors
            End If
        End Sub

        Private Sub moveIndicatorColorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles moveIndicatorColorButton.Click
            ' Open a color dialog.
            Dim dlg As New ColorDialog()
            dlg.Color = Me.moveIndicatorColorPanel.BackColor
            dlg.CustomColors = OptionsDialog.customColors

            ' Set the move indicator color based on that selection.
            If dlg.ShowDialog(Me) = DialogResult.OK Then
                Me.moveIndicatorColorPanel.BackColor = dlg.Color
                Me.moveIndicatorColorPanel.Refresh()
                OptionsDialog.customColors = dlg.CustomColors
            End If
        End Sub

        ' ===================================================================
        ' Event handlers for the form buttons.
        ' ===================================================================

        Private Sub restoreDefaultsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles restoreDefaultsButton.Click
            ' Reset the game options to their defaults.
            Me.Options.RestoreDefaults()

            ' Update the form controls.
            Me.MapOptionsToControls()
        End Sub

        Private Sub okButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles okButton.Click
            ' Set game options based on the form control values.
            Me.MapControlsToOptions()
        End Sub
    End Class
End Namespace